﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Collections;

namespace 摇奖器Demo
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// 用于显示信息的颜色变化
        /// </summary>
        Timer t1 = new Timer();
        /// <summary>
        /// 辅助颜色切换的布尔型变量
        /// </summary>
        Boolean b = true;
        // 用于摇奖
        Timer t2 = new Timer();
        /// <summary>
        /// 用来保存点击停止后，所摇到的数字所在的index
        /// </summary>
        public static int a = 0;
        public Form1()
        {
            InitializeComponent();
            LabelInfo_ColorChange();
        }
        /// <summary>
        /// 点击开始后的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            switch (button1.Text)
            {
                case "开始":
                    t1.Stop();
                    button1.Text = "停止";
                    StartYaoJiang();
                    break;
                case "停止":
                    t2.Stop();
                    Number.RemoveAt(a);
                    t1.Start();
                    button1.Text = "开始";
                    CheckPrice(label_info.Text);
                    break;
                default:
                    MessageBox.Show("系统错误，请联系开发者。");
                    break;
            }
        }

        /// <summary>
        /// 页面加载时的颜色切换事件，调用t1
        /// </summary>
        private void LabelInfo_ColorChange()
        {
            t1.Interval = 100;
            t1.Tick += new System.EventHandler(this.t_tick);
            t1.Start();
        }
        /// <summary>
        /// 判断是否中奖
        /// </summary>
        /// <param name="str1"></param>
        public void CheckPrice(string str1)
        {
            switch (str1)
            {
                case "88888":
                    ShowPrice("一等奖");
                    break;
                case "66668":
                case "66688":
                case "66888":
                case "68888":
                    ShowPrice("二等奖");
                    break;
                case "11111":
                case "22222":
                case "33333":
                case "44444":
                case "55555":
                case "66666":
                case "77777":
                case "99999":
                    ShowPrice("三等奖");
                    break;
                default:
                    ShowPrice("");
                    break;
            }
        }
        /// <summary>
        /// 显示摇号结果
        /// </summary>
        /// <param name="str"></param>
        public void ShowPrice(string str)
        {
            switch (str)
            {
                case "一等奖":
                    MessageBox.Show("恭喜您获得了本场唯一的一个一等奖！", "中奖结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case "二等奖":
                    MessageBox.Show("恭喜您获得了本场二等奖！", "中奖结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case "三等奖":
                    MessageBox.Show("恭喜您获得了本场三等奖！", "中奖结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case "":
                    MessageBox.Show("重在参与，欢迎再来！", "中奖结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
            }
        }
        /// <summary>
        /// 程序加载完成后t1每隔100ms的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void t_tick(object sender, EventArgs e)
        {
            b = !b;
            ColorChange(label_info, Color.Red, Color.Silver, b);
        }

        /// <summary>
        /// 给数字显示的label更换颜色
        /// </summary>
        /// <param name="l"></param>
        /// <param name="c1"></param>
        /// <param name="c2"></param>
        /// <param name="bol"></param>
        public void ColorChange(Label l, Color c1, Color c2, Boolean bol)
        {
            l.ForeColor = bol ? c1 : c2;
        }

        /// <summary>
        /// 开始摇号
        /// </summary>
        public void StartYaoJiang()
        {
            t2.Interval = 20;
            t2.Tick += new System.EventHandler(this.t_tick2);
            for (int i = 0; i < 100000; i++)
            {
                Number.Add(i);
            }
            t2.Start();
        }
        /// <summary>
        /// 点击开始后的计时器t2每个100ms的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void t_tick2(object sender, EventArgs e)
        {
            label_info.Text = random_Num(Number.Count);
            ColorChange(label_info, Color.Red, Color.Silver, true);
        }
        /// <summary>
        /// 保存所有被随机的数字
        /// </summary>
        public static ArrayList Number = new ArrayList();
        /// <summary>
        /// 产生随机数的种子
        /// </summary>
        public static Random Rd = new Random(0);
        /// <summary>
        /// 产生随机数
        /// </summary>
        public static string random_Num(int i)
        {
            string str = "";
            a = Rd.Next(0, i);
            str = Number[a].ToString().PadLeft(5, '0');
            return str;
        }

    }
}
